package org.nearby.note.dialog;

import java.awt.Component;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetAdapter;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DropTargetEvent;
import java.awt.dnd.DropTargetListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.filechooser.FileSystemView;

import org.nearby.note.Fastrun;
import org.nearby.note.bean.fastrun;
import org.nearby.note.dao.fastrunDaoImpl;

public class AddFastRunDlg extends JDialog {
    private JLabel showTip;
    private JTextField addText;
    private JButton addBtn;
    private JPanel panel1;
    private fastrunDaoImpl frdImpl = new fastrunDaoImpl();

    private Component _self;
    private Fastrun _parent;
    private String fileName;

    private ArrayList<Integer> runIds = new ArrayList<Integer>(); // 存储 添加的运行ids

    public AddFastRunDlg(JFrame parent, String title, boolean modal) {
        super(parent, title, modal);

        _self = this;
        _parent = (Fastrun) parent;

        initWin();
        initBind();
    }

    private void initWin() {
        panel1 = new JPanel();
        panel1.setLayout(null);

        addText = new JTextField("请输入要启动的程序路径");
        addText.setBounds(20, 20, 200, 30);

        addBtn = new JButton("添   加");
        addBtn.setFocusPainted(false);
        addBtn.setBounds(240, 20, 100, 30);

        showTip = new JLabel("把要添加的程序拖拽到此处");
        showTip.setBounds(100, 60, 200, 200);

        // 添加拖动源
        new DropTarget(showTip, DnDConstants.ACTION_LINK, new DropTargetAdapter() {
            @Override
            public void drop(DropTargetDropEvent dtde) {
                try {
                    // 接受文件、目录、快捷方式
                    if (dtde.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
                        dtde.acceptDrop(DnDConstants.ACTION_LINK);

                        List list = (List) (dtde.getTransferable()
                                .getTransferData(DataFlavor.javaFileListFlavor));
                        
                        Iterator iter = list.iterator();
                        while(iter.hasNext()){
                            File f = (File)iter.next();
                            addText.setText(f.getAbsolutePath());
                            fileName = f.getName();
                            
                        }
                        dtde.dropComplete(true);

                    }else{
                        JOptionPane.showMessageDialog(null, 2);
                        dtde.rejectDrop();
                    }
                } catch (UnsupportedFlavorException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });

        panel1.add(addText);
        panel1.add(addBtn);
        panel1.add(showTip);

        this.add(panel1);
        this.setLocationRelativeTo(null);
        this.setSize(400, 400);
    }

    private void initBind() {
        // 处理输入框
        addText.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (addText.getText().equals("请输入要启动的程序路径")) {
                    addText.setText("");
                }
            }
        });
        addBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                fastrun fr = new fastrun();
                if(addText.getText().equals("")){
                    JOptionPane.showMessageDialog(_self, "路径不能为空");
                    return;
                }
                if(fileName == null){
                    final String sep = System.getProperty("file.separator");
                    int lastIndex = addText.getText().lastIndexOf(sep);
                    fileName = addText.getText().substring(lastIndex + 1);
                }
                
                fr.setRun_name(fileName);
                fr.setRun_path(addText.getText());
                fr.setLastrun_datetime(null);
                int run_id = frdImpl.addFastRun(fr);
                if (run_id > 0) {
                    runIds.add(Integer.valueOf(run_id));
                    JOptionPane.showMessageDialog(_self, "添加成功");
                } else {
                    JOptionPane.showMessageDialog(_self, "添加失败");
                }
            }
        });
        this.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                _parent.updateTableUI(((AddFastRunDlg) _self).getRunIds());
                dispose();
            }
        });
    }

    public ArrayList<Integer> getRunIds() {
        return runIds;
    }
    /*
     * public static void main(String[] args){ new AddFastRunDlg(null, "启动", false); }
     */

}
